We will apply Montecarlo Simulations and Efficient Portfolio Management theory to Financial Time Series to determine how much of 5 Global eCommerce stocks we should buy for our investment portfolio. Namely, we will be looking at:
Mercado Libre (Latam), Amazon and Ebay (USA/Europe), Alibaba & JD.com (Asia/China)
Source: Yahoo Finance + All things R.
Individual assets and the market move in correlation and covariance with many additional financial and economical factors, each with its respective volatilities. I run a wide analysis on the mayor indexes and other possible influential players for each of the companies, countries and mayor markets where these companies operate.
As we can see from the graph below, as expected, the mayor influences on these eCommerce companies performance are: each other, of course, the market indexes of the US, the index for the markets in their countries of origin, and the indexes of the mayor markets they operate in.
All companies under our current coverage are traded in the US stock markets. Beyond this first introductory analysis and going forward, for our purposes of risk management and portfolio management, we will work with three key economic factors:
Notes: “X” marks denote a pairwise correlation rejected at a 0.05 sig.level.
Sources of variables: Yahoo Finance.
Lets begin by looking at how our individual stock prices and performance are tied to the US market. We will use historical returns starting on January of 2018 for MELI, AMZN, EBAY, BABA and JD.
Looking at daily arithmetic returns for each stock and the S&P500 minus the daily closing interest rate of the 10 year US treasury Bond, which we use as the risky free return, we compute each stock’s and the S&P500’s excess return. We then compare each of their excess returns versus the excess return of the market, represented by the S&P 500 index.
With alphas very close to zero, all stocks are fairly priced.
| MELI | AMZN | EBAY | BABA | JD | |
|---|---|---|---|---|---|
| Beta | 1.23 | 0.94 | 0.82 | 0.93 | 1.09 |
| Alpha | 0.00339 | 0.000868 | -0.000124 | -0.000238 | 0.00149 |
| Over/Under Priced | Under-priced | Neutral | Over-priced | Over-priced | Under-priced |
| Market Risk Exposure | 27.54% | 34.04% | 5.27% | 29.01% | 24.97% |
| Expected Excess Returns | -0.15% | 0.09% | 0.19% | 0.1% | -0.03% |
Decompose trend and seasonal factros with Seasonal Decomposition of Time Series by Loess
## Call:
## stl(x = meli.ts[, "MELI.Adjusted"], s.window = "periodic", robust = T)
##
## Time.series components:
## seasonal trend remainder
## Min. :-43.47121 Min. :115.8062 Min. :-193.73181
## 1st Qu.: -8.57296 1st Qu.:130.9593 1st Qu.: -18.12678
## Median : 2.00026 Median :278.0939 Median : -3.03494
## Mean : 0.65807 Mean :304.4755 Mean : -0.70110
## 3rd Qu.: 11.46687 3rd Qu.:434.6018 3rd Qu.: 17.51607
## Max. : 62.55654 Max. :648.6298 Max. : 196.31552
## IQR:
## STL.seasonal STL.trend STL.remainder data
## 20.04 303.64 35.64 237.54
## % 8.4 127.8 15.0 100.0
##
## Weights:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.8133 0.9452 0.8141 0.9888 1.0000
##
## Other components: List of 5
## $ win : Named num [1:3] 13661 379 253
## $ deg : Named int [1:3] 0 1 1
## $ jump : Named num [1:3] 1367 38 26
## $ inner: int 1
## $ outer: int 15
Note: I will set seed 123 at the very begining so that everyone’s randomizer can be set equally to reproduce the same or as close to the same results at any time the same data is used.
1 year is approximately 252 business/trading days, so we will forecast 252 periods ahead.
There are 3 similar methods (stl, stlm, and stlf), we will use stl.
dm.test(e1, e2, alternative=c(“two.sided”,“less”,“greater”), h=1, power=1)
e1 Forecast errors from method 1.
e2 Forecast errors from method 2.
A character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter.
The forecast horizon used in calculating e1 and e2 is determined with h.
The power used in the loss function. Usually 1 or 2. The choice of power is entirely due to the loss function. If we lose x dollars if the forecast error is x - then our loss function is linear and we should use the option power = 1. If we lose x^2 dollars when the forecast error is x, then we should use power = 2.
The null hypothesis is that the two methods have the same forecast accuracy.
For alternative=“two.sided”, the alternative hypothesis is that method 1 and method 2 have different levels of accuracy
For alternative=“less”, the alternative hypothesis is that method 2 is less accurate than method 1.
For alternative=“greater”, the alternative hypothesis is that method 2 is more accurate than method 1.
A smaller p-value means that there is stronger evidence in favor of the alternative hypothesis
##
## Diebold-Mariano Test
##
## data: stl.forecast.meli$residualsets.forecast.meli$residuals
## DM = 3.2277, Forecast horizon = 252, Loss function power = 1, p-value =
## 0.001278
## alternative hypothesis: two.sided
p-value is very low, H0 rejected, stl and ets have different levels of accuracy.
##
## Diebold-Mariano Test
##
## data: stl.forecast.meli$residualsets.forecast.meli$residuals
## DM = 3.2277, Forecast horizon = 252, Loss function power = 1, p-value =
## 0.0006388
## alternative hypothesis: greater
p-value = is very low, H0 rejected: ets IS MORE accurate than stl.
##
## Diebold-Mariano Test
##
## data: stl.forecast.meli$residualsets.forecast.meli$residuals
## DM = 3.2277, Forecast horizon = 252, Loss function power = 1, p-value =
## 0.9994
## alternative hypothesis: less
p-value = 1, H0 accepted: ets IS NOT LESS accurate than stl.
Lets use MONTECARLO simulations now adn run 20,000 simulations 252 trading days into the future.
Alternatively to auto.arima which can take very long and be very heavy on computational resources, there is the option to use auto ARFIMA out of the rugarcch library.
The Rugarch package is one of the more robust R libraries for financial data analysis and forecating.
It allows to forecast more than just prices, specifically volatility which is very importat for VaR estimations at the time to decide what porfoloio allocations are on the efficient fontier and which allocations will give us less than optimal returns for a given level of risk.
Even more so, it can accept streaming data and build it into the models, but we will not cover that here - it is just nice to know.
Sources: http://www.unstarched.net/r-examples/rugarch/a-short-introduction-to-the-rugarch-package/ https://palomar.home.ece.ust.hk/MAFS6010R_lectures/Rsession_time_series_modeling.html
## AR MA Mean ARFIMA AIC converged
## 1 0 0 1 0 -4.539027 1
## 2 0 1 1 0 -4.537928 1
## 3 1 0 1 0 -4.537011 1
## 4 0 1 0 0 -4.536723 1
## 5 0 2 1 0 -4.536519 1
## 6 2 0 1 0 -4.536493 1
So ARMA(0,0) model WITH a mean
Parameters for ARIMA(p,d,q)
If convergence problems arise:
In choosing ARMA(p,q) the theory of difference equations suggests that we should choose ARMA(p+1,p), which gives rise to a solution of the difference equation with p+1 terms. You can of course have some zero orders
This is explained in my book H. D. Vinod, “Hands-On Intermediate Econometrics Using R: Templates for Extending Dozens of Practical Examples.” (2008) World Scientific Publishers: Hackensack, NJ. (http://www.worldscibooks.com/economics/6895.html)
No convergence problems so we will use p and q with/without mean as estimated:
Run 20,000 simulations, 252 trading days into the future. Set seed 123 to be able to reproduce same results.
1 year (252 trading days approx.) target price for MERCADO LIBRE in USD 1349.41, (55.65% change compared to the last closing price of USD 866.93)
Makes sense!
1 year (252 trading days approx.) target price for AMAZON in USD 3894.01, (56.83% change compared to the last closing price of USD 2483)
Seems a little TOO bullish, but we have enough simulations on variation for the VaR that we will do further down.
1 year (252 trading days approx.) target price for EBAY in USD 65.69, (34.55% change compared to the last closing price of USD 48.82)
Makes sense, although the conditional sigma looks flat, we can double check that with the VaR excersise.
1 year (252 trading days approx.) target price for ALIBABA in USD 219.93, (0.17% change compared to the last closing price of USD 219.55)
Makes sense!
1 year (252 trading days approx.) target price for JD.com in USD 58.65, (-0.67% change compared to the last closing price of USD 59.04)
Makes sense!
The optimal or efficient portfolio mixes that follow were formulated using:
. Projected daily prices based on the simulations for all companies that we discussed earlier.
. Computing arithmetic daily returns of said projected prices.
. Limitation on the maximum portfolio participation of any single stock to not go above a ceiling of 40% long, and not below 20% short. I chose these constraints for arbitrary reasons: a rule of thumb of twice the amount of an equal weights portfolio (100% divided by 5) for long and no more than once the balanced portfolio for short positions.
. Sharpe ratio calculated using stocks daily simple returns in excess of risk free rate. . 13 week US Treasury Bill Risk Free rate
| MELI | AMZN | EBAY | BABA | JD | |
|---|---|---|---|---|---|
| Weights - Shorts Allowed, Max 50% allocation | 0.17 | 0.29 | 0.50 | 0.02 | 0.03 |
| Weights - No Shorts, Max 35% allocation | 0.24 | 0.35 | 0.35 | 0.03 | 0.03 |
Key points to keep in mind:
. VaR and ES are approximation measures to the worst that could happen (max losses) on one single day out of twenty (5%).
. ES looks at the entire first five percent quantile of the low end of the return distribution tail so its larger than VaR.
. With positive correlation, asset prices tend to move together and this increases the volatility.
. Without short sales, it is impossible to go above the expected return of the stock with the highest expected return.
. When short sales are allowed, there is no upper bound on the expected return nor on the risk. And the projected fall for Alibaba, a company with a market cap above 100B, should not be for the long run. A short sale strategy should be combined with a stop-loss limit order.
| Total | MELI | AMZN | EBAY | BABA | JD | |
|---|---|---|---|---|---|---|
| VaR(5%) - Short Sales Allowed | -14.05% | -2.98% | -5.18% | -5.89% | 0% | 0.01% |
| VaR(5%) - Short Sales Prohibited | -14.58% | -4.21% | -6.25% | -4.12% | 0% | 0.01% |
References: R Core Team (2015). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. <URL https://www.R-project.org/>. All Things R. Pull Yahoo Finance Key-Statistics Instantaneously Using XML and XPath in R. <URL: http://allthingsr.blogspot.com.ar/2012/10/pull-yahoo-finance-key-statistics.html>. Raymond McTaggart and Gergely Daroczi (2015). Quandl: API Wrapper for Quandl.com. R package version 2.6.0. <URL: http://CRAN.R-project.org/package=Quandl>. Taiyun Wei (2013). corrplot: Visualization of a correlation matrix. R package version 0.73. <URL: http://CRAN.R-project.org/package=corrplot>. Jeffrey A. Ryan (2015). quantmod: Quantitative Financial Modelling Framework. R package version 0.4-5. <URL: http://CRAN.R-project.org/package=quantmod>. Hyndman RJ (2015). forecast: Forecasting functions for time series and linear models. R package version 6.1, <URL: http://github.com/robjhyndman/forecast>. Hyndman RJ and Khandakar Y (2008). “Automatic time series forecasting: the forecast package for R.” Journal of Statistical Software, 26(3), pp. 1-22. <URL: http://ideas.repec.org/a/jss/jstsof/27i03.html>. Alexios Ghalanos (2015). rugarch: Univariate GARCH models. R package version 1.3-6. Andrew Matuszak. the Economist at Large. Efficient Frontier and plotEfficientFrontier. <URL: http://economistatlarge.com/>. David Ruppert. Statistics and Data Analysis for Financial Engineering (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY 10013, USA). John L. Weatherwax, PhD. A Solution Manual for: Statistics and Data Analysis for Financial Engineering by David Rupert. <URL: http://www.waxworksmath.com>. S original by Berwin A. Turlach R port by Andreas Weingessel Andreas.Weingessel@ci.tuwien.ac.at (2013). quadprog: Functions to solve Quadratic Programming Problems.. R package version 1.5-5. <URL: http://CRAN.R-project.org/package=quadprog>. Brian G. Peterson and Peter Carl (2014). PerformanceAnalytics: Econometric tools for performance and risk analysis. R package version 1.4.3541. <URL: http://CRAN.R-project.org/package=PerformanceAnalytics>. Yihui Xie (2015). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.11. Yihui Xie (2015) Dynamic Documents with R and knitr. 2nd edition. Chapman and Hall/CRC. ISBN 978-1498716963. Yihui Xie (2014) knitr: A Comprehensive Tool for Reproducible Research in R. In Victoria Stodden, Friedrich Leisch and Roger D. Peng, editors, Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595. JJ Allaire, Jeffrey Horner, Vicent Marti and Natacha Porte (2015). markdown: ‘Markdown’ Rendering for R. R package version 0.7.7. http://CRAN.R-project.org/package=markdown.